package com.itcast.security.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itcast.common.model.dto.request.CheckGroupAddReqDTO;
import com.itcast.common.model.dto.request.CheckGroupPullReqDTO;
import com.itcast.common.model.dto.request.CheckGroupReqDTO;
import com.itcast.common.model.dto.response.CheckGroupListResDTO;
import com.itcast.common.model.dto.response.CheckGroupResDTO;
import com.itcast.security.mapper.CheckGroupMapper;
import com.itcast.security.model.Checkgroup;
import com.itcast.security.service.ICheckGroupService;
import com.jzo2o.common.expcetions.BadRequestException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.mysql.utils.PageHelperUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class                                                            CheckGroupServiceImpl extends ServiceImpl<CheckGroupMapper, Checkgroup> implements ICheckGroupService {

    public PageResult<CheckGroupResDTO> queryList(CheckGroupReqDTO checkGroupReqDTO) {
        return PageHelperUtils.selectPage(checkGroupReqDTO,
                () -> baseMapper.queryList(checkGroupReqDTO.getName()));
    }

    /**
     * 添加检查组
     * @param checkGroupReqDTO
     * @return
     */
    @Override
    public Checkgroup add(CheckGroupAddReqDTO checkGroupReqDTO) {
        String groupName = checkGroupReqDTO.getGroupName();
        Long count1 = lambdaQuery()
                .eq(Checkgroup::getGroupName, groupName)
                .count();
        if(count1 > 0){
            throw new RuntimeException("检查组名称已存在");
        }
        Checkgroup checkgroup = BeanUtil.toBean(checkGroupReqDTO, Checkgroup.class);
        checkgroup.setCreateTime(LocalDateTime.now());

        int count = baseMapper.insert(checkgroup);
        if(count <= 0){
            throw new RuntimeException("添加检查组失败");
        }
        return checkgroup;
    }


    /**
     * 修改检查组信息
     * @param id
     * @param checkGroupAddReqDTO
     */
    @Override
    public Checkgroup updateCheckGroup(Long id, CheckGroupAddReqDTO checkGroupAddReqDTO) {
        String groupName = checkGroupAddReqDTO.getGroupName();
        Long count = lambdaQuery()
                .eq(Checkgroup::getGroupName, groupName)
                .ne(Checkgroup::getId, id)
                .count();
        if(count > 0){
            throw new BadRequestException("检查组名称已存在");
        }
        //根据id查询检查组信息
        Checkgroup checkgroup = getById(id);
        if(ObjectUtil.isNull(checkgroup)){
            throw new BadRequestException("检查组不存在");
        }
        checkgroup.setGroupName(groupName);
        checkgroup.setGroupDescription(checkGroupAddReqDTO.getGroupDescription());
        //更新检查组信息
        boolean flag = updateById(checkgroup);
        if(!flag){
            throw new BadRequestException("修改检查组信息失败");
        }
        return checkgroup;
    }


    /**
     * 启用
     *
     * @param id
     * @return
     */
    @Override
    public Checkgroup oNStatus(Long id) {
        Checkgroup checkgroup = getById(id);
        if(ObjectUtil.isNull(checkgroup)){
            throw new BadRequestException("检查组不存在");
        }
        checkgroup.setStatus(1);
        boolean flag = updateById(checkgroup);
        if(!flag){
            throw new BadRequestException("启用检查组失败");
        }
        return checkgroup;
    }

    @Override
    public Checkgroup offStatus(Long id) {
        Checkgroup checkgroup = getById(id);
        if(ObjectUtil.isNull(checkgroup)){
            throw new BadRequestException("检查组不存在");
        }
        checkgroup.setStatus(0);
        boolean flag = updateById(checkgroup);
        if(!flag){
            throw new BadRequestException("禁用检查组失败");
        }
        return checkgroup;
    }

    @Override
    public boolean removeCheckGroup(Long id) {
        Checkgroup checkgroup = getById(id);
        if(ObjectUtil.isNull(checkgroup)){
            throw new BadRequestException("检查组不存在");
        }
        Integer status = checkgroup.getStatus();
        if(status == 1){
            throw new BadRequestException("检查组已启用，无法删除");
        }
        return removeById(id);
    }

    /**
     * 下拉列表
     *
     * @param checkGroupPullReqDTO
     * @return
     */
    @Override
    public List<CheckGroupListResDTO> queryCheckList(CheckGroupPullReqDTO checkGroupPullReqDTO) {
        return baseMapper.queryCheckList(checkGroupPullReqDTO.getGroupStatus());
    }
}